/*
 * Copyright (c) 2018-2999 广州市蓝海创新科技有限公司 All rights reserved.
 *
 * https://www.mall4j.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */
package com.yami.shop.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yami.shop.bean.model.ShopRenovation;
import com.yami.shop.common.config.Constant;
import com.yami.shop.common.exception.YamiShopBindException;
import com.yami.shop.common.i18n.LanguageEnum;
import com.yami.shop.common.util.RedisUtil;
import com.yami.shop.dao.ShopRenovationMapper;
import com.yami.shop.service.ShopRenovationService;
import lombok.AllArgsConstructor;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 店铺装修信息
 *
 * @author lhd
 * @date 2021-01-05 11:03:38
 */
@Service
@AllArgsConstructor
public class ShopRenovationServiceImpl extends ServiceImpl<ShopRenovationMapper, ShopRenovation> implements ShopRenovationService {

    private final ShopRenovationMapper shopRenovationMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateToHomePage(ShopRenovation shopRenovation) {
        // 取消设为主页
        if(Objects.equals(shopRenovation.getHomeStatus(),1)){
            update(new LambdaUpdateWrapper<ShopRenovation>().set(ShopRenovation::getHomeStatus,0)
                    .eq(ShopRenovation::getRenovationId,shopRenovation.getRenovationId())
            );
            return;
        }

        // 设为主页的装修列表
        List<ShopRenovation> list = list(new LambdaQueryWrapper<ShopRenovation>().eq(ShopRenovation::getHomeStatus, 1)
                .eq(ShopRenovation::getShopId, shopRenovation.getShopId())
                .eq(ShopRenovation::getRenovationType, shopRenovation.getRenovationType()));

        // 先取消已有的主页
        update(new LambdaUpdateWrapper<ShopRenovation>().set(ShopRenovation::getHomeStatus,0)
                .eq(ShopRenovation::getHomeStatus,1)
                .eq(ShopRenovation::getShopId, shopRenovation.getShopId())
                .eq(ShopRenovation::getRenovationType, shopRenovation.getRenovationType())
        );
        // 设置当前模板为主页
        update(new LambdaUpdateWrapper<ShopRenovation>().set(ShopRenovation::getHomeStatus,1)
                .eq(ShopRenovation::getRenovationId, shopRenovation.getRenovationId()));
        // 清除旧的主页缓存
        if (CollUtil.isNotEmpty(list)) {
            List<String> key = new ArrayList<>();
            for (ShopRenovation renovation : list) {
                key.add("shopRenovationKey" + Constant.UNION + renovation.getRenovationId());
            }
            RedisUtil.del(key);
        }
    }

    @Override
    public void checkShopRenovation(ShopRenovation shopRenovation) {
        String content = shopRenovation.getContent();
        if (Objects.nonNull(content)) {
            JSONArray jsonArray = JSONUtil.parseArray(content);
            for (int i = 0; i < jsonArray.size(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                if (Objects.equals(jsonObject.getStr("type"), "imageAd")) {
                    JSONObject jsonObject2 = jsonObject.getJSONObject("dataField");
                    if (Objects.nonNull(jsonObject2)) {
                        JSONArray jsonArray2 = jsonObject2.getJSONArray("imageList");
                        if (Objects.nonNull(jsonArray2)) {
                            if (jsonArray2.size() > 10) {
                                throw new YamiShopBindException("yami.h5.index.ad.images.limit");
                            }
                        }
                    }
                }
            }
        }
    }

    @Override
    @Cacheable(cacheNames="shopRenovationHomepageKey",key="#shopId + ':' + #renovationType")
    public ShopRenovation homeShopRenovation(Long shopId, Integer renovationType) {
        ShopRenovation shopRenovation = getOne(new LambdaQueryWrapper<ShopRenovation>()
                .eq(ShopRenovation::getShopId, shopId)
                .eq(ShopRenovation::getHomeStatus, 1)
                .eq(ShopRenovation::getRenovationType, renovationType)
        );
        return shopRenovation;
    }

    @Override
    @Cacheable(cacheNames="shopRenovationKey",key="#renovationId")
    public ShopRenovation getShopRenovationById(Long renovationId) {
        return getById(renovationId);
    }

    @Override
    public void removeCache(Long shopId, Integer renovationType, Long renovationId) {
        List<String> key = new ArrayList<>();
        key.add("shopRenovationHomepageKey" + Constant.UNION + shopId + Constant.COLON + renovationType);
        key.add("shopRenovationKey" + Constant.UNION + renovationId);
        RedisUtil.del(key);
    }
}
